/*
* Created by: yyf
* Description: 母基金管理--投资中母基金 详情
*/
<template>
    <div>
        <div class="right">
            <div>
                <el-col :span="24" class="ca-align-right">
                    <div class="ca-head-fixed" v-if="fund.fundState==4">
                        <el-button type="primary" @click="toQuitFund()" :disabled="!isAuthDetailChange">转到已退出母基金</el-button>
                    </div>
                </el-col>
                <!--头部详情-->
                <div class="ca-box ca-p-20" v-if="!isEdit">
                    <el-row v-if="!isEdit">
                        <el-col class="pull-right ca-align-right">
                            <el-button type="text" class="ca-edit-btn el-icon-edit" @click="changeHeadEdit(true)" :disabled="!isAuthDetailEdit">编辑</el-button>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="6" class="ca-border-r ca-m-r-40 ca-p-r-20 ca-align-center">
                            <h3 class="ca-title-fullname wordBreak">{{this.fund.fundFullname}}</h3>
                            <h4 class="ca-title-name wordBreak">({{this.fund.fundName}})</h4>
                            <div><span class="investmentBtn">{{this.fund.fundState | getNameById('fundState')}}</span></div>
                        </el-col>
                        <el-col :span="16">
                            <el-row class="ca-leng-28 ca-head-font">
                                <el-col :span="12">
                                    <el-row>已投金额(万元)：{{this.fundHead.investmentMoney | format1}}</el-row>
                                    <el-row>存续期(年)：{{this.fund.investmentPeriod}} + {{this.fund.exitPeriod}}</el-row>
                                </el-col>
                                <el-col :span="12">
                                    <el-row>已投基金(支)：{{this.fundHead.investmentCast}}</el-row>
                                    <el-row>已退出基金(支)：{{this.fundHead.exitFund}}</el-row>
                                </el-col>
                            </el-row>
                        </el-col>
                    </el-row>
                </div>
                <!--头部编辑-->
                <div class="ca-box ca-p-20" v-if="isEdit">
                    <el-row>
                        <el-col class="ca-align-right ca-m-b-10 ca-m-t-20">
                            <el-button type="primary" size="medium" @click="save()">保存</el-button>
                            <el-button size="medium" @click="changeHeadEdit(false)">取消</el-button>
                        </el-col>
                    </el-row>
                    <el-form ref="fundForm" :rules="rules" :model="fund">
                        <el-row>
                            <el-col :span="11">
                                <el-form-item label="基金全称" label-width="120px" prop="fundFullname">
                                    <el-input v-model="fund.fundFullname" :maxlength="100"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="11" :offset="1">
                                <el-form-item label="基金简称" label-width="100px" prop="fundName">
                                    <el-input v-model="fund.fundName" :maxlength="100"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                </div>
            </div>
            <el-tabs type="border-card"
                     class="ca-m-t-15 ca-tabs"
                     @tab-click="selectTab"
                     :value="tabIndex">
                <el-tab-pane label="母基金详情" :disabled="!isAuthDetailSee">
                    <csRedDot slot="label" label="母基金详情" :showDot="dotStateList[0]"></csRedDot>
                    <div v-if="tabIndex == 0">
                        <csDetail :fund="fund"
                        :fundMother="fundMother"
                        :region="region"
                        @addRedDot="addRedDot(0)"
                        @getFundsDetail="getFundsDetail"
                        @changeRegion="changeRegion">
                        </csDetail>
                    </div>
                </el-tab-pane>
                <el-tab-pane label="合伙人认缴" :disabled="!isAuthPartnerSee">
                    <csRedDot slot="label" label="合伙人认缴" :showDot="dotStateList[1]"></csRedDot>
                    <div v-if="tabIndex == 1">
                        <csPartnerPaid :fundId="itemId" :isMotherFund="true" @addRedDot="addRedDot(1)" :isAuth="isAuthPartnerEdit"></csPartnerPaid>
                    </div>
                </el-tab-pane>
                <el-tab-pane label="投资数据" :disabled="!isAuthInvestmentSee">
                    <csRedDot slot="label" label="投资数据" :showDot="dotStateList[2]"></csRedDot>
                    <div v-if="tabIndex == 2">
                        <csInvestment></csInvestment>
                    </div>
                </el-tab-pane>
                <el-tab-pane label="统计分析" :disabled="!isAuthAnalysisSee">
                    <csRedDot slot="label" label="统计分析" :showDot="dotStateList[3]"></csRedDot>
                    <div v-if="tabIndex == 3">
                        <csStatistc></csStatistc>
                    </div>
                </el-tab-pane>
                <el-tab-pane label="子基金回款" :disabled="!isAuthPaybackSee">
                    <csRedDot slot="label" label="子基金回款" :showDot="dotStateList[4]"></csRedDot>
                    <div v-if="tabIndex == 4">
                        <csPayback></csPayback>
                    </div>
                </el-tab-pane>
                <el-tab-pane label="基金分配" :disabled="!isAuthDistributionSee">
                    <csRedDot slot="label" label="基金分配" :showDot="dotStateList[5]"></csRedDot>
                    <div v-if="tabIndex == 5">
                        <csFundsDistribution :fundId="itemId" :isMotherFund="true" @addRedDot="addRedDot(5)" :isAuth="isAuthDistributionEdit"></csFundsDistribution>
                    </div>
                </el-tab-pane>
                <el-tab-pane label="文档" :disabled="!isAuthFileSee">
                    <csRedDot slot="label" label="文档" :showDot="dotStateList[6]"></csRedDot>
                    <div v-if="tabIndex == 6">
                        <csFundFileDetail
                            title="基金文档："
                            :isShowFileChildType="true"
                            :isEstablish="true"
                            :businessId="itemId"
                            :fileType="fileType"
                            :addAuth="pageName"
                            @addDot="addRedDot(6)">
                        </csFundFileDetail>
                    </div>
                </el-tab-pane>
            </el-tabs>
        </div>
    </div>
</template>
<script type="text/ecmascript-6">
        import {api} from './api';
        import {STATUS_HTTP_SUCCESS, apiHttp} from '__service/csHttp';
        import { addRemind } from '__service/utils';
        import { getDetailRemind, read } from '__service/utils';
        import { isNoAuth } from '__service/utils';
    export default {
        data() {
            return {
                tabIndex: this.$route.params.tabIndex.toString(),
                itemId: this.$route.params.itemId,
                fileType: '',
                pageName: '投资中母基金-文档-编辑',
                isEdit: false, // 头部编辑状态
                fundHead: {
                    investmentMoney: '',
                    investmentCast: '',
                    exitFund: '',
                },
                fund: { // 母基金基金基础信息
                    establishTime: '',   // 拟设立日期
                    exitPeriod: '',      // 退出期
                    exitTime: '',        // 退出时间
                    fundCategory: '',    // 基金类别
                    fundFullname: '',    // 基金全称
                    fundName: '',        // 基金简称
                    fundScale: null,     // 基金规模
                    fundState: 4,     // 设立状态
                    fundType: '',         // 基金类型
                    id: '',               // 基金主id
                    investmentPeriod: '', // 投资期
                    treeType: '2'          // 1：子基金；2：母基金 对应字典表：fundTree
                },
                fundMother: { // 母基金详情
                    backups: '',          // 是否备案
                    brief: '',            // 基金简介 ,
                    chairman: '',         // 董事长
                    custodian: '',        // 基金托管人
                    fundId: '',           // 基金基础表id
                    generalManager: '',   // 总经理
                    id: '',               // 主id
                    manageFee: '',        // 基金管理费
                    managementCompany: '',// 管理公司
                    manager: '',          // 基金管理人
                    managerName: '',      // 基金经理
                    supervisor: ''        // 监事会主席
                },
                region: [ // 注册地点
                    {
                        businessId: '',    // 业务id
                        businessType: null,// 业务类型(1:注册地，2：办公地)
                        city: '',           // 市
                        district: '',       // 区
                        id: '',             // id
                        province: '',       // 省
                        registrationDetailed: '',//详细地址
                    }
                ],
                dotStateList: [false, false, false, false, false, false, false], // 小红点状态
                rules: {
                    fundFullname: [
                        { required: true, message: '请输入基金全称', trigger: 'blur' },
                    ],
                    fundName: [
                        { required: true, message: '请输入基金简称', trigger: 'blur' },
                    ],
                },
                tabArr: ['母基金详情', '合伙人认缴', '投资数据', '统计分析', '子基金回款', '基金分配', '文档'],//tab名称
                isAuthDetailEdit: isNoAuth('投资中母基金-母基金详情-编辑'),
                isAuthDetailChange: isNoAuth('投资中母基金-母基金详情-一键转已退出'),
                isAuthPartnerEdit: '投资中母基金-合伙人认缴-编辑',
                isAuthDetailSee: isNoAuth('投资中母基金-母基金详情-查看'),
                isAuthPartnerSee: isNoAuth('投资中母基金-合伙人认缴-查看'),
                isAuthInvestmentSee: isNoAuth('投资中母基金-投资数据-查看'),
                isAuthAnalysisSee: isNoAuth('投资中母基金-统计分析-查看'),
                isAuthPaybackSee: isNoAuth('投资中母基金-子基金回款-查看'),
                isAuthDistributionSee: isNoAuth('投资中母基金-基金分配-查看'),
                isAuthFileSee: isNoAuth('投资中母基金-文档-查看'),
                isAuthDistributionEdit: '投资中母基金-基金分配-编辑',
                isAuthFileEdit: isNoAuth('投资中母基金-文档-编辑'),
            }
        },
        props: {},
        beforeRouteEnter(to, from, next) {
            next( vm => {
                // 遍历所有tab 权限
                if (!isNoAuth('投资中母基金-' + vm.tabArr[vm.tabIndex] + '-查看')) {
                    let flag = false;
                    for (let i = vm.tabIndex; i < vm.tabArr.length; i++) {
                        if (isNoAuth('投资中母基金-' + vm.tabArr[i] + '-查看')) {
                            vm.tabIndex = i.toString();
                            flag = true;
                            break;
                        }
                    }
                    if (!flag) {
                        for (let i = 0; i < vm.tabIndex; i++) {
                            if (isNoAuth('投资中母基金-' + vm.tabArr[i] + '-查看')) {
                                vm.tabIndex = i.toString();
                                flag = true;
                                break;
                            }
                        }
                    }
                    if (!flag) {
                        vm.$message({
                            message: '没有此页面的操作权限',
                            type: 'error',
                            center: true
                        });
                        vm.$router.replace({name: 'personal-center', params: {tabIndex: '0', openWindow: false}})
                    }
                }
            })
        },
        created() {
            // 获取母基金头部 + 详情
            this.getFundsDetail();
            let fileTypeList = this.$store.state.user.fileFloderDicVoList;
            fileTypeList.map(item=>{
                if (item.name === '基金文档') {
                    this.fileType = item.type;
                }
            })
        },
        mounted() {
            this.getDotList()
            // 为子组件创建的第三方组件方法
            this.$root.Dot.$on('getDotList', () => {
                this.getDotList()
            })
        },
        methods: {
            async addRedDot(index) {
                // 添加小红点
                await addRemind({
                    dataId: this.$route.params.itemId, // 业务id
                    dataTab: index, // tab页顺序 head 也为0 新建为'-1'
                    dataType: 3, // 1拟投资项目,2已投资项目,3母基金,4拟设立基金,5已设立基金,6客户,7潜在项目
                });
                // 根据第三方组件来调用 父组件中定义的方法 重新获取小红点
                this.$root.Dot.$emit('getDotList');
                // 添加小红点 end
            },
            // 改变头部编辑状态
            changeHeadEdit(val) {
                this.isEdit = val;
            },
            // 获取详情
            async getFundsDetail() {
                let {resCode, data} = await apiHttp(api.FUNDS_DETAIL, {id: this.itemId});
                if (resCode === STATUS_HTTP_SUCCESS) {
                    this.fundHead.exitFund = data.exitFund;
                    this.fundHead.investmentCast = data.investmentCast;
                    this.fundHead.investmentMoney = data.investmentMoney;
                    this.fund = data.fund;
                    this.fundMother = data.fundMother;
                    this.region = data.region;
                }
            },
            // 改变tabIndex
            selectTab(tab) {
                this.tabIndex = tab.index;
                // 去除小红点
                read({ dataId: this.$route.params.itemId, dataTab: this.tabIndex, dataType: 3 });
                this.dotStateList[this.tabIndex] = false;
                this.dotStateList = this.dotStateList.concat([]);
            },
            // 获取小红点
            async getDotList() {
                // 获取小红点
                const dotList = await getDetailRemind({ dataId: this.$route.params.itemId, dataType: 3 });
                dotList.map(x => {
                    this.dotStateList[x.dataTab] = true;
                });
                this.dotStateList = this.dotStateList.concat([]);
            },
            // 保存头部信息 + 详情
            save() {
                this.$refs.fundForm.validate(async (valid) => {
                    if (valid) {
                        let {resCode} = await apiHttp(api.EDIT_FUND_MOTHER, {
                            fund: this.fund,
                            fundMother: this.fundMother,
                            region: this.region
                        });
                        if (resCode === STATUS_HTTP_SUCCESS) {
                            this.$message({
                                type: 'success',
                                message: '修改成功！',
                                center: true
                            })
                            this.addRedDot(0);
                        }
                        this.changeHeadEdit(false);
                    }
                })
            },
            // 改变注册地点
            changeRegion(val) {
                this.region = [];
                this.region.push({
                    businessId: this.itemId,
                    businessType: val.businessType,
                    city: val.city,               // 市
                    district: val.district,       // 区
                    id: '',                       // id
                    province: val.province,       // 省
                    registrationDetailed: val.registrationDetailed,  // 详细地址
                })
            },
            // 转到已退出
             toQuitFund() {
                this.$confirm('是否转成已退出母基金', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    showClose: false
                }).then(async () => {
                    this.fund.fundState = 5; // 已退出基金状态
                    let {resCode} = await apiHttp(api.EDIT_FUND_MOTHER, {
                        fund: this.fund,
                        fundMother: this.fundMother,
                        region: this.region
                    });
                    if (resCode === STATUS_HTTP_SUCCESS) {
                        // 修改成功后跳转到已退出列表页
                        this.$router.push({name: 'quit-FOF-list', params: {openWindow: false}})
                    }
                })
            },
        },
        components: {
            'csDetail': require('./detail/index'), // 母基金详情
            'csPartnerPaid': require('../invested-funds/partner-paid/index'), // 合伙人认缴
            'csInvestment': require('./investment/index'), // 投资数据
            'csPayback': require('./payback/index'), // 子基金回款
             'csFundsDistribution': require('../invested-funds/fund-distribution/index'), // 基金分配
            'csFundFileDetail': require('__components/file-list/index'), // 基金文件详情
            'csStatistc': require('./statistic-analysis/index'), // 统计分析
            'csRedDot': require('__components/red-dot'), // 选项卡小红点
        }
    }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
    .ca-title-fullname {
        font-size: 18px;
        line-height: 26px;
        color: #333;
    }
    .ca-title-name {
        font-size: 14px;
        line-height: 20px;
        color: #666;
        font-weight: normal;
    }
    .wordBreak{
        word-break: break-all;
    }
    .ca-head-font .el-row{
        font-size:14px;
        color: #313131;
    }
    .ca-head-font span {
        font-size: 14px;
    }
    .investmentBtn {
        padding: 0 5px;
        display: inline-block;
        height: 22px;
        color:#fff;
        line-height:22px;
        text-align: center;
        margin-top:6px;
        background: #1a85e1;
    }
</style>
